<?php
require CWF_PATH.'libs/Smarty/Smarty.class.php';
//控制器基类
class Controller{
	//定义属性，保存smarty class
	private $_view=null;
	function __construct()
	{
		$this->_view=new Smarty;
		$this->_view->template_dir=array(TPL_PATH,APP_PATH.'Public/Tpl');
		$this->_view->compile_dir=APP_PATH.APP_NAME.'/Templates_c';
		is_dir($this->_view->compile_dir) or mkdir($this->_view->compile_dir,0755,true);
	}
	public function __set($var,$value){
		$this->assign($var,$value);
	}
	/**
	 * 取smarty 里的assign方法
	 * 分配变量
	 * @param  [type] $var   [description]
	 * @param  [type] $value [description]
	 * @return [type]        [description]
	 */
	public function assign($var,$value){
		$this->_view->assign($var,$value);
	}
	public function display($tpl=null)
	{
		if (is_null($tpl)) {
			$tpl=strtolower(METHOD).'.html';
		}else{
			$tpl=substr($tpl,-5)=='.html'?$tpl:$tpl.'.html';
		}
		$this->_view->display($tpl);
	}

    /**
     * Ajax输出
     * @param $data 数据
     * @param string $type 数据类型 text html xml json
     */
    protected function ajax($data, $type = "JSON")
    {
        $type = strtoupper($type);
        switch ($type) {
            case "HTML":
            case "TEXT":
                $_data = $data;
                break;
            case "XML": //XML处理
                $_data = Xml::create($data, "root", "UTF-8");
                break;
            default: //JSON处理
                $_data = json_encode($data);
        }
        echo $_data;
        exit;
    }
    
	/**
	 * 成功时显示方法
	 * @param  [type] $msg [description]
	 * @return [type]      [description]
	 */
	protected function success($message,$url=''){
		$this->assign('message',$message);
		$this->assign('url',$url);
		$this->display('success.html');
	}
	protected function error($message,$url=''){
		$this->assign('message',$message);
		$this->assign('url',$url);
		$this->display('error.html');
		exit;
	}

}